int allst[MAXM], M = 0; // 压位表示 int mapp[MAXT]= {0}; voidDFS(int p, int state, int maxi){ if (p == K + 1) { allst[++ M] = state; mapp[state] = M; return ; } for (int i = 1; i <= min (maxi + 1, K); i ++) { int nst = state * 10 + i; DFS (p + 1, nst, max (i, maxi)); } }
structMatrix { LL a[MAXM][MAXM];
Matrix () { for (int i = 1; i <= M; i ++) for (int j = 1; j <= M; j ++) a[i][j] = 0; } } ; Matrix operator * (const Matrix& A, const Matrix& B) { Matrix ret = Matrix (); for (int i = 1; i <= M; i ++) for (int j = 1; j <= M; j ++) for (int k = 1; k <= M; k ++) ret.a[i][j] = (ret.a[i][j] + A.a[i][k] * B.a[k][j] % MOD) % MOD; return ret; } Matrix f, g; LL matpower(LL p){ while (p) { if (p & 1) f = f * g; g = g * g; p >>= 1; } return f.a[1][1]; }
int father[MAXK]; intfind(int x){ return father[x] == x ? x : father[x] = find (father[x]); } LL power(LL x, int p){ if (p < 0) return1; LL cnt = 1; while (p) { if (p & 1) cnt = cnt * x % MOD; x = x * x % MOD; p >>= 1; } return cnt; } int visit[MAXK]= {0}, bit[MAXK]= {0}; voidmaintain(int state, int con){ for (int i = 1; i <= K + 1; i ++) father[i] = i, visit[i] = 0; for (int st = state, i = K; i > 0; i --, st /= 10) bit[i] = st % 10; for (int i = 1; i <= K; i ++) for (int j = i + 1; j <= K; j ++) { int fx = find (i), fy = find (j); if (bit[i] != bit[j] || fx == fy) continue; father[fx] = fy; } for (int j = 1; j <= K; j ++) if (con & (1 << (j - 1))) { int fx = find (K + 1), fy = find (j); if (fx == fy) return ; father[fx] = fy; } bool fail = true; for (int i = 2; i <= K + 1; i ++) if (find (1) == find (i)) { fail = false; break; } if (fail) return ; // 之前的点都联通 for (int i = 1; i <= K; i ++) bit[i] = find (i + 1); int cnt = 0; for (int i = 1; i <= K; i ++) if (! visit[bit[i]]) visit[bit[i]] = ++ cnt; intfinal = 0; for (int i = 1; i <= K; i ++) final = final * 10 + visit[bit[i]]; g.a[mapp[state]][mapp[final]] ++, g.a[mapp[state]][mapp[final]] %= MOD; } int cnt[MAXK]= {0}; voidprep(){ for (int i = 1; i <= M; i ++) { // i in [1, k] for (int j = 1; j <= K; j ++) cnt[j] = 0; int st = allst[i]; for (int j = 1; j <= K; j ++, st /= 10) cnt[st % 10] ++; LL ret = 1; for (int j = 1; j <= K && cnt[j] > 0; j ++) ret = ret * power (cnt[j], cnt[j] - 2) % MOD; f.a[1][i] = ret; } int limit = (1 << K) - 1; for (int i = 1; i <= M; i ++) // 枚举当前点及其与之前的连通状态 for (int state = 0; state <= limit; state ++) maintain (allst[i], state); /*for (int i = 1; i <= M; i ++) { for (int j = 1; j <= M; j ++) cout << g.a[i][j] << ' '; puts (""); }*/ }
intmain(){ cin >> K >> N; DFS (1, 0, 0), prep (); LL ans = matpower (N - K); cout << ans << endl;